<?php
namespace app\houtai\controller;
use think\Controller;//引入Controller控制器类
use Request;
use Env;
use app\index\model\Channel;
use app\index\model\Pageinfo;
use app\index\model\Article as Art;
class Article extends Controller//文章管理
{
    /**
     * 初始化操作
     */
     protected function initialize()
     {
        isLogin();
        $channel = Channel::all();
        $this->assign('channel',$channel);
     }
    /**
     * 默认操作，对应文章管理页
     */
    public function index($chanid='')
    {
        
        if(!$chanid) {
            $article = Art::all();
        } else {//当前栏目id存在，则显示当前栏目及其子栏目的文章列表
            $chan = Channel::get($chanid);//获取当前栏目信息
            $data = [];//用于存储当前栏目及其子栏目的id
            $i =0;
            $result = Channel::all();
            foreach($result as $v)
            {
                if(strpos($v->path,(string)($chanid)) !== false) {
                    $data[$i++] = $v->id;
                }
            }
            $article = Art::where('cid','in',$data)->select();
        }
        $this->assign('article',$article);
        return $this->fetch();
    }
    /*
     * 新增操作
    */
    public function add()
    {
        $channel = Channel::all();
        $arr = $this->tree($channel);
        $this->assign('channel',$arr);
        return $this->fetch();
    }
    /*
    **新增校验操作
    */
    public function addChk()
    {
        //dump(Request::param());
        //1.表单校验：自行完成，具体可借鉴前台注册功能的表单校验
        //2.已添加的文章不能再添加
        $name = Request::param('name');
        if (Art::get(['name' => $name])) {
            $this -> error('该文章已存在');
        }
        //3.上传文章缩略图
        /*$file = Request::file('thumb');
        $config = ['size'=>'5242880','ext'=>'jpg,gif,png'];
        $path = Env::get('ROOT_PATH').'public/static/img/thumb';
        $info = $file -> validate($config)->move($path);
        if(!$info) {
            return $file->getError();
        } else {
            $image = \think\Image::open($info->getPathName());
            //按照原图的比例生成一个最大为150*150的缩略图并覆盖原图
            $image -> thumb(150,150) -> save($info -> getPathName());
            $thumb = 'static/img/thumb' . $info -> getSaveName();//图片存放路径（用于写入数据库）
        }*/
        //4.获取表单信息
        $cid = Request::param('cid');
        $intr = Request::param('intr');
        $title = Request::param('title');
        $keywords = Request::param('keywords');
        $description = Request::param('description');
        $content = Request::param('content');
        //5.pageinfo新增数据
        $data = [
            'name' => $name,
            'title' => $title,
            'keywords' => $keywords,
            'description' => $description,
        ];
        $result = Pageinfo::create($data);
        if(!$result) {
            $this -> error('SEO信息添加失败');
        }
        //6.article新增数据
        $data = [
            'name' => $name,
            'pageid' => $result->id,//pageid对应pageinfo的id
            'cid' => $cid,
            //'thumb' => $thumb,
            'intr' => $intr,
            'description' => $description,
            'content' => $content,
        ];
        $result = Art::create($data);
        if(!$result) {
            $this -> error('文章信息添加失败');
        } else {
            $this -> success('文章添加成功','/houtai.php/article');
        }
    }
    /*
    **栏目层级树
    **$data：栏目信息
    **$pid：父栏目id
    **$level：当前栏目层级
    */
    public function tree($data,$pid = 0,$level = 1)
    {
        static $arr=[];
        foreach($data as $k => $v) {
            if($v['pid'] == $pid) {
                $v['name'] = str_repeat('|--',$level) . $v['name'];
                $arr[] = $v;
                $this->tree($data,$v['id'],$level+1);
            }
        }
        return $arr;
    }
}
?>